package furnitures 
{
	import adobe.utils.CustomActions;
	import flash.geom.Point;
	import furnitures.parts.FaceBox;
	import furnitures.parts.FaceImage;
	import furnitures.parts.Foots;
	import furnitures.parts.Handle;
	import utils.Logger;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class FurnitureModel 
	{
		/**
		 * shape at looking from up
		 */
		public var shapePoints:Array;
		/**
		 * Represents anchor curve. X - relative, values 1..0; Y- absolute value
		 * Start and End points for curve calculate from furniture's width and depth
		 */
		public var faceAnchorCurve:Point = null;
		public var faces:Vector.<FurnitureFace>;
		
		public var foots:Foots;
		
		public var furnitureHeight:Number;
		public var furnitureWidth:Number;
		public var furnitureDepth:Number = 570;
		public var positionY:Number = 0;
		public var hasTabletop:Boolean = false;
		public var title:String = "";
		
		/**
		 * Полки, значения - относительные позиции по высоте
		 */
		public var shelves:Vector.<Number>;
		
		/**
		 * Форсированная отрисовка полок
		 */
		public var forceShelves:Boolean = false;
		
		//public var rigthSide:Boolean = false;
		public var side:String ;
		
		public var shapePointsSide:String;
		
		public static const SHAPE_SIDE_TOP:String = "top";
		public static const SHAPE_SIDE_LEFT:String = "left";
		public static const SHAPE_SIDE_FRONT:String = "front";
		
		public var extra:String = "";
		
		public static const EXTRA_GLASS:String = "glass";
		public static const EXTRA_GRID:String = "grid";
		//public static const EXTRA_NONE:String = "none";
		public static const EXTRA_NO_FACE:String = "no_face";
		public static const EXTRA_NORMAL:String = "normal";
		
		public static const SIDE_LEFT:String = "left";
		public static const SIDE_RIGHT:String = "right";
		
		
		public static const ITEM_FURNITURE:String = "furniture";
		public static const ITEM_TECH:String = "tech";
		
		
		public var itemType:String = ITEM_FURNITURE;
		
		public var doubleSided:Boolean = false;
		/**
		 * Spacing for glass/grid, mm
		 */
		public static const EXTRA_SPACING:Number = 50;
		public function FurnitureModel() 
		{
			shapePointsSide = SHAPE_SIDE_TOP;
			side = SIDE_RIGHT;
		}
		
		public function addFace(face:FurnitureFace):void
		{
			if (faces == null)
			{
				faces = new Vector.<FurnitureFace>();
			}
			faces.push(face);
		}
		
		public function getSizesNeeded():Vector.<String>
		{
			var sizesNeeded:Vector.<String> = new Vector.<String>();
			
			if (extra == FurnitureModel.EXTRA_NO_FACE) {
				return sizesNeeded;
			}
			
			for each(var face:FurnitureFace in faces)
			{
				for each(var door:FaceBox in face.doors)
				{
					sizesNeeded.push(getDoorSize(door));
				}
			}
			
			return sizesNeeded;
		}
		
		public function getDoorSize(door:FaceBox):String
		{
			if (!door.allowExtra) {
				return door.sizeType;
			}
			
			switch(extra)
			{
				case EXTRA_GLASS:
						return door.sizeType + "+glass";
					break;
				case EXTRA_GRID:
						return door.sizeType + "+grid";
					break;
				default:
						return door.sizeType;
					break;
			}
			return door.sizeType;
		}
		
		public function clone():FurnitureModel
		{
			var cloneFurn:FurnitureModel = new FurnitureModel();
			var i:int;
			if (shapePoints != null)
			{
				cloneFurn.shapePoints = [];
				var pointsXY:Array;
				var pointsXYClone:Array;
				for (i = 0; i < shapePoints.length; i++)
				{
					pointsXY = shapePoints[i];
					pointsXYClone = [];
					for (var j:int = 0; j < pointsXY.length; j++)
					{
						pointsXYClone.push(pointsXY[j]);
						//log(i + ", " + j + " = " + pointsXY[j]);
					}
					cloneFurn.shapePoints.push(pointsXYClone);
				}
			}
			
			if (faceAnchorCurve != null)
			{
				cloneFurn.faceAnchorCurve = faceAnchorCurve.clone();
			}
			
			if (faces != null)
			{
				for (i = 0; i < faces.length; i++)
				{
					cloneFurn.addFace(faces[i].clone());
				}
			}
			
			if (foots != null)
			{
				cloneFurn.foots = foots.clone();
			}
			
			if (shelves != null) {
				cloneFurn.shelves = new Vector.<Number>();
				for (i = 0 ; i < shelves.length; i++ ) {
					cloneFurn.shelves.push(shelves[i]);
				}
				cloneFurn.forceShelves = forceShelves;
			}
			
			cloneFurn.furnitureWidth = furnitureWidth;
			cloneFurn.furnitureHeight = furnitureHeight;
			cloneFurn.furnitureDepth = furnitureDepth;
			
			cloneFurn.positionY = positionY;
			cloneFurn.title = title;
			cloneFurn.hasTabletop = hasTabletop;
			cloneFurn.side = side;
			cloneFurn.extra = extra;
			cloneFurn.itemType = itemType;
			
			cloneFurn.shapePointsSide = shapePointsSide;
			cloneFurn.doubleSided = doubleSided;
			return cloneFurn;
		}
		
		private function log(msg:String):void
		{
			Logger.log(this, msg );
		}
	}

}